package cn.itcast.rabbitmq.spring;

import cn.itcast.utils.MailUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import java.util.Map;

public class EmailMqListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        try {
            byte[] body = message.getBody();
//          借助阿里巴巴的一个工具类把字节数组转成map
            Map<String,String> emailMap = JSON.parseObject(body, Map.class);
            String to = emailMap.get("to");
            String subject = emailMap.get("subject");
            String content = emailMap.get("content");
            MailUtil.sendMsg(to,subject,content);
            System.out.println("邮件发送成功");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
